﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CARMSBusiness;
using CARMSBusiness.InterFace;
using CARMSData.EntityClasses;

namespace CARMS.Admin
{
    [Function("Admin.UsePermission")]
    public partial class UserPermissionSet : PageBase
    {
        private int CurrentUserId
        {
            get
            {
                return GetIntByParameter("UserId");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDefault();
                LoadDefault();
            }
        }

        private void BindDefault()
        {
            ControlHelper.BindPublicItemsByTypeID(chkEventList, DictionaryCategoryConst.ButtonPermissionID, EmptyType.None);
            ControlHelper.BindReportCategoryList(listReport, EmptyType.None);
            ControlHelper.BindPublicItemsByTypeID(chkReportStatus, DictionaryCategoryConst.ReportStatusID, EmptyType.None);

        }

        private void LoadDefault()
        {



        }
        protected void listReport_OnSelectedIndexChanged(object sendre, EventArgs e)
        {
            if (listReport.SelectedValue.Trim().Length > 0)
            {
                chkLimitSender.Checked = false;
                chkLimitChecker.Checked = false;
                foreach (ListItem item in chkEventList.Items)
                {
                    item.Selected = false;
                }
                foreach (ListItem item in chkReportStatus.Items)
                {
                    item.Selected = false;
                }
                var entity = new UserPermissionEntity(CurrentUserId, int.Parse(listReport.SelectedValue));
                if (!entity.IsNew)
                {
                    chkLimitSender.Checked = entity.LimitSender;
                    chkLimitChecker.Checked = entity.LimitChecker;

                    foreach (ListItem item in chkEventList.Items)
                    {
                        if (item.Value == DictionaryCategoryConst.ButtonPermission.Add.ToString() && entity.Added)
                        {
                            item.Selected = true;
                        }
                        if (item.Value == DictionaryCategoryConst.ButtonPermission.Change.ToString() && entity.Changes)
                        {
                            item.Selected = true;
                        }
                        if (item.Value == DictionaryCategoryConst.ButtonPermission.Confirm.ToString() && entity.ToConfirm)
                        {
                            item.Selected = true;
                        }
                        if (item.Value == DictionaryCategoryConst.ButtonPermission.Remove.ToString() && entity.Remove)
                        {
                            item.Selected = true;
                        }
                        if (item.Value == DictionaryCategoryConst.ButtonPermission.Export.ToString() && entity.Export)
                        {
                            item.Selected = true;
                        }
                        if (item.Value == DictionaryCategoryConst.ButtonPermission.Print.ToString() && entity.Printing)
                        {
                            item.Selected = true;
                        }
                    }

                    var statusList = entity.AllowStatus.SeperateString('|');
                    foreach (var status in statusList)
                    {
                        foreach (ListItem item in chkReportStatus.Items)
                        {
                            if(item.Value==status)
                            {
                                item.Selected = true;
                            }
                        }
                    }
                }
            }
        }

        protected void btnSavePermission_Click(object sendre, EventArgs e)
        {
            if (listReport.SelectedIndex == -1)
            {
                Alert("Please select a report!");
                return;
            }
            var entity = new UserPermissionEntity(CurrentUserId, int.Parse(listReport.SelectedValue));
            if (entity.IsNew)
            {
                entity.UserId = CurrentUserId;
                entity.ReportId = int.Parse(listReport.SelectedValue);
            }
            entity.LimitSender = chkLimitSender.Checked;
            entity.LimitChecker = chkLimitChecker.Checked;

            //初始化
            entity.Added = false;
            entity.Changes = false;
            entity.ToConfirm = false;
            entity.Remove = false;
            entity.Export = false;
            entity.Printing = false;

            foreach (ListItem item in chkEventList.Items)
            {
                if (item.Value == DictionaryCategoryConst.ButtonPermission.Add.ToString() && item.Selected)
                {
                    entity.Added = true;
                }

                if (item.Value == DictionaryCategoryConst.ButtonPermission.Change.ToString() && item.Selected)
                {
                    entity.Changes = true;
                }

                if (item.Value == DictionaryCategoryConst.ButtonPermission.Confirm.ToString() && item.Selected)
                {
                    entity.ToConfirm = true;
                }

                if (item.Value == DictionaryCategoryConst.ButtonPermission.Remove.ToString() && item.Selected)
                {
                    entity.Remove = true;
                }

                if (item.Value == DictionaryCategoryConst.ButtonPermission.Export.ToString() && item.Selected)
                {
                    entity.Export = true;
                }

                if (item.Value == DictionaryCategoryConst.ButtonPermission.Print.ToString() && item.Selected)
                {
                    entity.Printing = true;
                }
            }

            var reportStatusList = new List<string>();
            foreach (ListItem item in chkReportStatus.Items)
            {
                if (item.Selected)
                {
                    reportStatusList.Add(item.Value);
                }
            }

            entity.AllowStatus = reportStatusList.SeperateListToString("|");

            if (entity.Save())
            {
                Alert(GenerateAlert.SavSuccess);
                //RediretResolveUrl("~/admin/UserManage.aspx");
            }
            else
            {
                Alert(GenerateAlert.SaveFailed);
            }
        }

        protected void btnReturn_Click(object sendre, EventArgs e)
        {
            RediretResolveUrl("~/admin/UserManage.aspx");
        }
    }
}
